﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace PixelWarfare
{
    /// <summary>
    /// A class with generic useful methods.
    /// </summary>
    static class Utility
    {
        /// <summary>
        /// Draw the String centred in the middle of the screen with an
        /// optional x and y offset and size.
        /// </summary>
        /// <param name="g">Graphics to draw on.</param>
        /// <param name="text">Text to draw.</param>
        /// <param name="color">Color of the text.</param>
        /// <param name="yOffset">Optional yOffset, default is centre.</param>
        /// <param name="xOffset">Optional xOffset, default is centre.</param>
        /// <param name="size">Size of the font to be drawn.</param>
        public static void DrawTextCentered(Graphics g, string text, Color color, 
            int yOffset = 0, int xOffset = 0, int size = 30)
        {
            Font font = new Font("arial", size);
            SizeF bounds = g.MeasureString(text, font); // So we know where to centre.

            g.DrawString(text, font, new SolidBrush(color),
                250 - (int)bounds.Width / 2 + xOffset, 250 - (int)bounds.Height / 2 + yOffset);
        }

        /// <summary>
        /// Draw the outer bar for the given player in the correct position that is percent full.
        /// </summary>
        /// <param name="g">Graphics to draw on.</param>
        /// <param name="player">Player number so we know where to draw the bar.</param>
        /// <param name="percent">Percent the bar is full.</param>
        /// <param name="color">Color of the bar.</param>
        public static void DrawBar(Graphics g, int player, double percent, Color color)
        {
            // Nothing to draw if his bar is empty.
            if (percent <= 0)
                return;

            // Size in pixels of the bar.
            int size = (int)(percent * 400);

            // Draw left hand side bar for first player.
            if (player == 0)
            {
                // Draw the black outline then the bar itself.
                g.FillRectangle(new SolidBrush(Color.Black),
                   23, 48, 14, size + 4);
                g.FillRectangle(new SolidBrush(color),
                    25, 50, 10, size);
            }
            // Draw bar on top for second player.
            else if (player == 1)
            {
                g.FillRectangle(new SolidBrush(Color.Black),
                  48 + (400 - size), 23, 404 - (400 - size), 14);
                g.FillRectangle(new SolidBrush(color),
                   50 + (400 - size), 25, 400 - (400 - size), 10);
            }
            // Draw bar on right hand side for third player.
            else if (player == 2)
            {
                g.FillRectangle(new SolidBrush(Color.Black),
                   463, 48 + (400 - size), 14, 404 - (400 - size));
                g.FillRectangle(new SolidBrush(color),
                   465, 50 + (400 - size), 10, 400 - (400 - size));
            }
            // Draw the bottom bar for the last player.
            else
            {
                g.FillRectangle(new SolidBrush(Color.Black),
                    48, 463, size + 4, 14);
                g.FillRectangle(new SolidBrush(color),
                    50, 465, size, 10);
            }

        }

        /// <summary>
        /// Returns a color with its RGB values darkened by a fixed amount.
        /// </summary>
        /// <param name="color">Color to darken.</param>
        /// <param name="amount">Amount to darken by.</param>
        /// <returns>Darkened color.</returns>
        public static Color Darken(Color color, int amount)
        {
            return Color.FromArgb(color.A, Math.Max(color.R - amount, 0),
                Math.Max(color.G - amount, 0), Math.Max(color.B - amount, 0));
        }

        /// <summary>
        /// Return a color from the given color with its RGB values
        /// multiplied by percent.
        /// 
        /// For example Derken((R-256, G-128, B-0), 0.5)
        /// would return (R-128, G-64, B-0).
        /// </summary>
        /// <param name="color">Color to be darkened.</param>
        /// <param name="percent">Percent to darken color.</param>
        /// <returns>Darkened Color.</returns>
        public static Color Darken(Color color, double percent)
        {
            int x = 0;
            if (color == Color.Purple)
                x++;

            int r = (int)(color.R * percent);
            int g = (int)(color.G * percent);
            int b = (int)(color.B * percent);

            return Color.FromArgb(r, g, b);
        }
    }
}
